//
//  DDMainMusicCell.m
//  DDMusic
//
//  Created by rimi on 16/11/9.
//  Copyright © 2016年 FengBaoFamily. All rights reserved.
//

#import "DDMainMusicCell.h"
#import "UILabel+DDUILabelCategory.h"
#import "UIButton+DDUIButtonCategory.h"
#import <Masonry.h>
#import "DDPublicMacro.h"
#import <UIButton+WebCache.h>
#import "DDMusicModel.h"

@interface DDMainMusicCell()
@property (nonatomic,strong) UILabel* musicTypeLabel;//歌曲类型label
@property (nonatomic,strong) UIButton* allMusicBtn;//所有歌曲按钮
#pragma mark  HotMusicStyleUI相关属性
@property (nonatomic,strong) NSMutableArray<UIButton*>* hotMusicImageBtnArr;//热门歌曲的三个按钮
@property (nonatomic,strong) NSMutableArray<UILabel*>* hotMusicNameLabelArr;//热门歌曲名字的三个label
@property (nonatomic,strong) NSMutableArray<UILabel*>* hotMusicSingerNameLabelArr;//热门歌曲歌手名label


@end
@implementation DDMainMusicCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor whiteColor];
        [self layoutUI];
    }
    return self;
}
#pragma mark  布局ui
- (void)layoutUI{
//musicTypeLabel
    self.musicTypeLabel = [UILabel dd_CreatLabelWithTitle:@"" font:21 TitleColor:[UIColor blackColor] TextAlignment:(NSTextAlignmentLeft)];
    self.musicTypeLabel.numberOfLines = 2;
    [self.contentView addSubview:self.musicTypeLabel];
    [self.musicTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@8);
        make.left.equalTo(@8);
    }];
    
//allMusicBtn
    
    self.allMusicBtn = [UIButton dd_creatButtonTitle:@"全部 >" buttonType:(UIButtonTypeCustom) font:15 titieColor:[UIColor lightGrayColor] bgColor:nil nomalImage:nil bgImage:nil];
    [self.contentView addSubview:self.allMusicBtn];
    __weak DDMainMusicCell* weakSelf = self;
   [self.allMusicBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       make.centerY.equalTo(weakSelf.musicTypeLabel.mas_centerY);
       make.width.equalTo(@60);
       make.height.equalTo(@20);
       make.right.equalTo(@(-8));
   }];
    


}
#pragma mark HotMusicStyleUI
- (void)setHotMusicStyleUI{
    self.hotMusicImageBtnArr = [NSMutableArray array];
    self.hotMusicNameLabelArr = [NSMutableArray array];
    self.hotMusicSingerNameLabelArr = [NSMutableArray array];
    NSInteger pitch = 10;
    NSInteger imageWidth = (SCREEN_SIZE.width - pitch*4)/3;
    for (NSInteger i = 0; i < 3; i++) {
        UIButton* imageBtn = [[UIButton alloc]init];
        [imageBtn addTarget:self action:@selector(hotMusicImageBtnClicked:) forControlEvents:(UIControlEventTouchUpInside)];
        __weak DDMainMusicCell* weakSelf = self;
        [self.contentView addSubview:imageBtn];
        [imageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(@(imageWidth));
            make.top.equalTo(weakSelf.allMusicBtn.mas_bottom).offset(pitch);
            make.left.equalTo(@(pitch+(imageWidth+pitch)*i));
        }];
        [self.hotMusicImageBtnArr addObject:imageBtn];
        UILabel* musicNameLabel = [UILabel dd_CreatLabelWithTitle:@"" font:17 TitleColor:[UIColor blackColor] TextAlignment:(NSTextAlignmentLeft)];
        [self.contentView addSubview:musicNameLabel];
        [musicNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(imageBtn.mas_left);
            make.right.equalTo(imageBtn.mas_right);
            make.top.equalTo(imageBtn.mas_bottom).offset(4);
        }];
        [self.hotMusicNameLabelArr addObject:musicNameLabel];
 
        UILabel* singerNameLabel = [UILabel dd_CreatLabelWithTitle:@"" font:14 TitleColor:[UIColor blackColor] TextAlignment:(NSTextAlignmentLeft)];
        [self.hotMusicSingerNameLabelArr addObject:singerNameLabel];
        [self.contentView addSubview:singerNameLabel];
        [singerNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(musicNameLabel.mas_left);
            make.right.equalTo(musicNameLabel.mas_right);
            make.top.equalTo(musicNameLabel.mas_bottom).offset(4);
        }];
        
    }
    UIView* bottomView = [[UIView alloc]init];
    bottomView.backgroundColor = [UIColor colorWithRed:246/255.0 green:246/255.0 blue:246/255.0 alpha:1];
    [self.contentView addSubview:bottomView];
    __weak DDMainMusicCell* weakSelf = self;
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.height.equalTo(@10);
        make.top.equalTo(weakSelf.hotMusicSingerNameLabelArr[0].mas_bottom).offset(8);
        make.bottom.equalTo(@0);
    }];
    
}
#pragma mark RecommendMusicStyleUI
- (void)setRecommendMusicStyle{
//recommendMusicTableView
    self.recommendMusicTableView = [[DDMainRankTableView alloc]init];
    [self.contentView addSubview:self.recommendMusicTableView];
  __weak  DDMainMusicCell* weakSelf = self;
    [self.recommendMusicTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(weakSelf.musicTypeLabel.mas_bottom).offset(16);
        make.height.equalTo(@240);
        make.bottom.equalTo(@0);
    }];

}

#pragma mark 根据cell样式更新ui
- (void)setStyle:(DDMainMusicCellStyle)style{
    _style = style;
    switch (_style) {
        case HotMusicStyle:
            [self setHotMusicStyleUI];
            break;
           case RecommendMusicStyle:
            [self setRecommendMusicStyle];
        default:
            break;
    }
}
#pragma mark 赋值数据
- (void)setViewModel:(DDMainMusicCellViewModel *)viewModel{
    _viewModel = viewModel;
    if (self.style == HotMusicStyle) {
        self.musicTypeLabel.text = @"热门歌曲";
        __weak DDMainMusicCell* weakSelf = self;
        [_viewModel.hotMusicModels enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (idx >= 2) {
                *stop = YES;
            }
            DDMusicModel* musicModel = (DDMusicModel*)obj;
            
            weakSelf.hotMusicNameLabelArr[idx].text =musicModel.songname;
            weakSelf.hotMusicSingerNameLabelArr[idx].text = musicModel.singername;
            [weakSelf.hotMusicImageBtnArr[idx] sd_setImageWithURL:[NSURL URLWithString:musicModel.albumpic_big] forState:(UIControlStateNormal)];
            self.recommendMusicTableView.viewModel = _viewModel;

            
        }];
        
        
    }else if (self.style == RecommendMusicStyle){
        self.musicTypeLabel.text = @"推荐排行";
        self.recommendMusicTableView.viewModel = _viewModel;
            [self.recommendMusicTableView reloadData];
       
        
    }

}
#pragma mark 关于HotMusicStyle的逻辑操作
#pragma mark 热门歌曲按钮的点击回调方法
- (void)hotMusicImageBtnClicked:(UIButton*)button{
   __weak DDMainMusicCell* weakSelf = self;
    [self.hotMusicImageBtnArr enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj == button) {
         [[NSNotificationCenter defaultCenter]postNotificationName:@"DDMainRankTableViewCellClicked" object:@{ISPLAYNEWMUSIC:@"YES",NEWPLAYMUSICLIST:@[weakSelf.viewModel.hotMusicModels[idx]]}];
        }
    }];
  
}
@end
